package repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import domain.IncomeRecord;

@Repository(value="incomeRecordRepository")
public class IncomeRecordRepositoryJpaImpl implements IncomeRecordRepository {
	@PersistenceContext
	private EntityManager em;

	public IncomeRecordRepositoryJpaImpl() {}

	public IncomeRecordRepositoryJpaImpl(EntityManager em) {
		this.em = em;
	}

	public IncomeRecord findById(int id) {
		return em.find(IncomeRecord.class, id);
	}

	public IncomeRecord find(IncomeRecord record) {
		return em.find(IncomeRecord.class, record.getId());
	}

	public List<IncomeRecord> findAll() {
		TypedQuery<IncomeRecord> query = em.createNamedQuery("IncomeRecord.findAll",
				IncomeRecord.class);
		return query.getResultList(); 
	}

	@Transactional
	public void create(IncomeRecord record) {
		  em.persist(record);
	}

	@Transactional
	public void delete(IncomeRecord record) {
			  em.remove(findById(record.getId()));
	}

	@Transactional
	public void deleteById(int id) {
			  em.remove(findById(id));
	}

	@Transactional
	public void update(IncomeRecord record) {
			  em.merge(record);
	}

}
